package com.shop.milk_tea_shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.shop.milk_tea_shop.common.CodeEnum;
import com.shop.milk_tea_shop.common.GlobalInfo;
import com.shop.milk_tea_shop.common.Result;
import com.shop.milk_tea_shop.entity.ItemAdd;
import com.shop.milk_tea_shop.entity.ItemAddCategory;
import com.shop.milk_tea_shop.mapper.ItemAddCategoryMapper;
import com.shop.milk_tea_shop.mapper.ItemAddMapper;
import com.shop.milk_tea_shop.request.ItemAddsQueryRequest;
import com.shop.milk_tea_shop.service.ItemAddCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 商品附加信息分类表：	商品的附加信息，温度、甜度、小料等 服务实现类
 * </p>
 *
 * @author yjw
 * @since 2024-06-16
 */
@Service
public class ItemAddCategoryServiceImpl extends ServiceImpl<ItemAddCategoryMapper, ItemAddCategory> implements ItemAddCategoryService {

    @Autowired
    ItemAddCategoryMapper itemAddCategoryMapper;

    @Autowired
    ItemAddMapper itemAddMapper;

    @Override
    public List<ItemAddCategory> getMenu(ItemAddsQueryRequest request) {
        LambdaQueryWrapper<ItemAddCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(request.getAddCategoryId() != null, ItemAddCategory::getAddCategoryId, request.getAddCategoryId());
        queryWrapper.like(StringUtils.isNotBlank(request.getAddCategoryName()), ItemAddCategory::getAddCategoryName, request.getAddCategoryName());
        queryWrapper.eq(request.getStatus() != null, ItemAddCategory::getStatus, request.getStatus());
        return itemAddCategoryMapper.selectList(queryWrapper);
    }

    /**
     * 删除分类的同时，需要删除该分类下的所有信息
     * @param addCategoryId
     * @return
     */
    @Override
    @Transactional
    public Result<Boolean> deleteCategoryAndAdds(Long addCategoryId) {
        ItemAddCategory addCategory = itemAddCategoryMapper.selectById(addCategoryId);
        addCategory.setStatus(0);
        itemAddCategoryMapper.updateById(addCategory);

        LambdaQueryWrapper<ItemAdd> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(addCategoryId != null, ItemAdd::getAddCategoryId, addCategoryId);
        List<ItemAdd> itemAdds = itemAddMapper.selectList(queryWrapper);
        itemAdds.forEach(add -> {
            add.setStatus(0);
        });
        itemAddMapper.updateById(itemAdds);
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, true, new GlobalInfo());
    }

    /**
     * 新增分类的同时，需要新增具体的附加信息
     * @param itemAddCategory
     * @param itemAdd
     * @return
     */
    @Override
    @Transactional
    public Result<Boolean> insertCategoryAndAddInfo(ItemAddCategory itemAddCategory, ItemAdd itemAdd) {
        itemAddCategoryMapper.insert(itemAddCategory);
        itemAddMapper.insert(itemAdd);
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, true, new GlobalInfo());
    }
}
